package com.example.demo421.controller;

import ch.qos.logback.core.encoder.EchoEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequestMapping("/test")
@RestController
public class TestController{
    private static EchoEncoder encoder = new EchoEncoder();
    @Autowired
    private RestTemplate laxRestTemplate;
    @GetMapping("/hmac")
    public void hmacAuth() {
        // 客户 API Key
        final String customerAPIKey = "输入有效的APIKey";
        // 客户 API Secret
        final String customerAPISecret = "输入有效的APISecret";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        String signedHeaders = "x-jcc-timestamp;x-jcc-service";
        long time = System.currentTimeMillis();
        String service = "jcc-api";
        String date = date(time);
        String signatureString = date + "/" + service;
        String sign = null;
        try {
            sign = HMACSHA256(signatureString, customerAPISecret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String auth = "J-HMAC-SHA256 key-id=\"" + customerAPIKey + "\"," +
                "signed-headers=\"" + signedHeaders + "\",signature=\"" + sign + "\"";
        httpHeaders.add("X-Jcc-Timestamp", time / 1000 + "");
        httpHeaders.add("X-Jcc-Service", "jcc-api");
        httpHeaders.add("X-Jcc-Authorization", auth);
        String body = "{\n" +
                "            \"templateId\":\"T78d350d15\",\n" +
                "        \"templateParams\":[\n" +
                "           \"121234\"], \n" +
                "      \"to\":\"+13137992197\",\n" +
                "      \"bizId\":\"123\"\n" +
                "      }";;
        String urlString = "https://sms.api.juphoon.com/sms/v1";
        HttpEntity requestEntity = new HttpEntity<>(body, httpHeaders);
        ResponseEntity<String> response = laxRestTemplate.exchange(urlString, HttpMethod.POST, requestEntity, String.class);
    }
    public static String HMACSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        String encode = String.valueOf(encoder.encode(array));
        return URLEncoder.encode(encode, "UTF-8");
    }
    public static String date(Long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
}